package www.openteam.com.adapter.realtime;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.EvaPaperBean;
import www.openteam.com.bean.EvalutateAddOrderBean;
import www.openteam.com.tool.AdpUtil;

//纸张表格信息
public class EvaPaperAdp extends RecyclerView.Adapter<EvaPaperAdp.ViewHolder> {
    Context context;
    List<EvaPaperBean> beanList;
    List<String> numbList;



    public EvaPaperAdp(Context context) {
        this.context = context;
        beanList = new ArrayList<>();
        EvaPaperBean evaPaperBean = new EvaPaperBean("0", "0", "0", "0",
                "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0",
                "0", "0", "0", "0");
        beanList.add(evaPaperBean);
    }

    public void setNumbList(List<String> numbList) {
        this.numbList = numbList;
    }

    public List<EvaPaperBean> getBeanList() {
        return beanList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_eva_paper_msg, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


        EvaPaperBean evaPaperBean = beanList.get(position);
        EvaPaperBean bean = new EvaPaperBean();
        EvaPaperBean baseBean = new EvaPaperBean("0", "0", "0", "0",
                "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0",
                "0", "0", "0", "0", "0",
                "0", "0", "0", "0");

        AdpUtil.setEditTextWatcher(holder.evaluatePaperPart, s -> bean.setPaperPart(s.toString()),
                () -> evaPaperBean.getPaperPart());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperUnit, s -> bean.setPaperUnit(s.toString()),
                () -> evaPaperBean.getPaperUnit());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperTypeId, s -> bean.setPaperTypeId(s.toString()),
                () -> evaPaperBean.getPaperTypeId());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperCost, s -> bean.setPaperCost(s.toString()),
                () -> evaPaperBean.getPaperCost());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperCostRate, s -> bean.setPaperCostRate(s.toString()),
                () -> evaPaperBean.getPaperCostRate());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperRemark, s -> bean.setPaperRemark(s.toString()),
                () -> evaPaperBean.getPaperRemark());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperOpenNumb, s -> bean.setPaperOpenNumb(s.toString()),
                () -> evaPaperBean.getPaperOpenNumb());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperSlitWidth, s -> bean.setPaperSlitWidth(s.toString()),
                () -> evaPaperBean.getPaperSlitWidth());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperSlitLength, s -> bean.setPaperSlitLength(s.toString()),
                () -> evaPaperBean.getPaperSlitLength());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperMinPaper, s -> bean.setPaperMinPaper(s.toString()),
                () -> evaPaperBean.getPaperMinPaper());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperPaperAddExtra, s -> bean.setPaperPaperAddExtra(s.toString()),
                () -> evaPaperBean.getPaperPaperAddExtra());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperPaperAdd, s -> bean.setPaperPaperAdd(s.toString()),
                () -> evaPaperBean.getPaperPaperAdd());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperLargePaper, s -> bean.setPaperLargePaper(s.toString()),
                () -> evaPaperBean.getPaperLargePaper());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperComposition, s -> bean.setPaperComposition(s.toString()),
                () -> evaPaperBean.getPaperPart());
        AdpUtil.setEditTextWatcher(holder.evaluatePaperWidth, s -> bean.setPaperWidth(s.toString()),
                () -> evaPaperBean.getPaperWidth());

        AdpUtil.setSpinnerAdp(holder.evaluateSpPaperCost,numbList,context);
        AdpUtil.setSpinnerAdp(holder.evaluateSpPaperMaCost,numbList,context);
        if (position == beanList.size() - 1) {
            holder.evaluatePaperAdd.setVisibility(View.VISIBLE);
        } else {
            holder.evaluatePaperAdd.setVisibility(View.GONE);
        }

        holder.evaluatePaperAdd.setOnClickListener(v -> {
            if (beanList.size() <= 10) {
                //最多只能新增10个
                beanList.add(position + 1, baseBean);
                notifyDataSetChanged();
                notifyItemRemoved(position + 1);
            }
        });

        holder.evaluatePaperDelete.setOnClickListener(v -> {
            if (beanList.size() > 1) {
                //留一个不删除
                beanList.remove(position);
                notifyDataSetChanged();
                notifyItemRemoved(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

//    private void setSpinnerAdp(Spinner spinner) {
//        List<String> adpList = new ArrayList<>();
//        if (addOrderList != null) {
//            for (EvalutateAddOrderBean bean : addOrderList) {
//                adpList.add(bean.getNumb() + "");
//            }
//        } else {
//            adpList.add("0");
//        }
//        ArrayAdapter<String> operatoAdapter = new ArrayAdapter<String>(context,
//                android.R.layout.simple_spinner_dropdown_item, adpList);
//        spinner.setAdapter(operatoAdapter);
//    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.evaluate_paper_part)
        EditText evaluatePaperPart;
        @Bind(R.id.evaluate_paper_unit)
        EditText evaluatePaperUnit;
        @Bind(R.id.evaluate_paper_typeId)
        EditText evaluatePaperTypeId;
        @Bind(R.id.evaluate_paper_type)
        EditText evaluatePaperType;
        @Bind(R.id.evaluate_paper_code)
        EditText evaluatePaperCode;
        @Bind(R.id.evaluate_paper_sizeId)
        EditText evaluatePaperSizeId;
        @Bind(R.id.evaluate_paper_name)
        EditText evaluatePaperName;
        @Bind(R.id.evaluate_paper_unitPrice)
        EditText evaluatePaperUnitPrice;
        @Bind(R.id.evaluate_paper_brand)
        EditText evaluatePaperBrand;
        @Bind(R.id.evaluate_paper_gramWeight)
        EditText evaluatePaperGramWeight;
        @Bind(R.id.evaluate_paper_length)
        EditText evaluatePaperLength;
        @Bind(R.id.evaluate_paper_width)
        EditText evaluatePaperWidth;
        @Bind(R.id.evaluate_paper_composition)
        EditText evaluatePaperComposition;
        @Bind(R.id.evaluate_paper_largePaper)
        EditText evaluatePaperLargePaper;
        @Bind(R.id.evaluate_paper_paperAdd)
        EditText evaluatePaperPaperAdd;
        @Bind(R.id.evaluate_paper_paperAddExtra)
        EditText evaluatePaperPaperAddExtra;
        @Bind(R.id.evaluate_paper_minPaper)
        EditText evaluatePaperMinPaper;
        @Bind(R.id.evaluate_paper_costRate)
        EditText evaluatePaperCostRate;
        @Bind(R.id.evaluate_paper_slitLength)
        EditText evaluatePaperSlitLength;
        @Bind(R.id.evaluate_paper_slitWidth)
        EditText evaluatePaperSlitWidth;
        @Bind(R.id.evaluate_paper_openNumb)
        EditText evaluatePaperOpenNumb;
        @Bind(R.id.evaluate_paper_remark)
        EditText evaluatePaperRemark;
        @Bind(R.id.evaluate_paper_cost)
        EditText evaluatePaperCost;
        @Bind(R.id.evaluate_paper_update)
        Button evaluatePaperUpdate;
        @Bind(R.id.evaluate_paper_delete)
        Button evaluatePaperDelete;
        @Bind(R.id.evaluate_paper_add)
        Button evaluatePaperAdd;
        @Bind(R.id.evaluate_SpPaper_cost)
        Spinner evaluateSpPaperCost;
        @Bind(R.id.evaluate_SpPaper_MaCost)
        Spinner evaluateSpPaperMaCost;
        @Bind(R.id.evaluate_paper_MaCost)
        EditText evaluatePaperMaCost;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
